package api.expertInfo;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import common.BaseController;
import common.CommonResult;
import common.validateUser.InterfaceUsers;
import common.validateUser.ValidateUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorInfoServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.*;
import services.staff.StaffInfoServices;
import util.Selector;
import validator.IdCardFluentValidator;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.*;
import wt.staticData.ShortData;
import wt.webPlugin.ZTree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpApi")
public class ExpertDetailInfoController extends BaseController {
    @Autowired
    public InterfaceUsers interfaceUsers;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    InspectorInfoServices InspectorInfoServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    ExpertCompanyServices expertCompanyServices;
    @Autowired
    ExpertWorkServices expertWorkServices;
    @Autowired
    ExpertEducationServices expertEducationServices;
    @Autowired
    ExpertReviewServices expertReviewServices;
    @Autowired
    ExpertTrainServices expertTrainServices;
    @Autowired
    ExpertMajorServices expertMajorServices;




    /**
     * 获取专家的详细信息
     * @param uid
     * @param timeStamp
     * @param sign
     * @param idCard
     * @param baseInfo
     * @param company
     * @param certificate
     * @param major
     * @param education
     * @param work
     * @param review
     * @param train
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "getExpertDetailInfo", produces = "application/json;charset=UTF-8")
    public CommonResult getExpertDetailInfo(
            String uid,
            Long timeStamp,
            String sign,
            String idCard,
            String baseInfo,
            String company,
            String certificate,
            String major,
            String education,
            String work,
            String review,
            String train
    ) throws Exception {


        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(uid, new NotBlankFluentValidator("uid"))
                .on(timeStamp, new NotNullFluentValidator("timeStamp"))
                .on(sign, new NotBlankFluentValidator("sign"))
                .on(idCard, new IdCardFluentValidator("idCard"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            CommonResult commonResult = new CommonResult();
            commonResult.addMessage(complexResult);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        CommonResult commonResult = ValidateUserUtil.validateUser(uid, timeStamp, sign, interfaceUsers);  //用户验证
        if (!CommonResult.ZJ001.equals(commonResult.getCode())) {
            return commonResult;
        }


        ExpertBase expertBase = expertBaseServices.selectByIdCard(idCard);
        if (expertBase == null) {
            commonResult.setMessage(CommonResult.ZJ005_M+"idCard");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();

        if ("1".equals(baseInfo)) {
            Map map = new HashMap();
            map.put("name", expertBase.getName());
            map.put("idCard", expertBase.getIdCard());
            map.put("sex", new Selector<Short,String>().putCatalog(ShortData.s0,"女").putCatalog(ShortData.s1,"男").run(expertBase.getSex()));
            map.put("phone", expertBase.getPhone());
            map.put("email", expertBase.getEmail());
            map.put("bankCard", expertBase.getBankCard());
            map.put("bankName", expertBase.getBankName());
            map.put("retire",  new Selector<Short,String>().putCatalog(ShortData.s0,"在职").putCatalog(ShortData.s1,"退休").run(expertBase.getRetire()));
            map.put("teacher", new Selector<Short,String>().putCatalog(ShortData.s0,"不是").putCatalog(ShortData.s1,"是").run(expertBase.getTeacher()));
            map.put("outLander", new Selector<Short,String>().putCatalog(ShortData.s0,"省内").putCatalog(ShortData.s1,"省外").run(expertBase.getTeacher()));
            map.put("education",StaticConstant.EDUCATION_MAP.get(expertBase.getEducation()));
            map.put("rank", StaticConstant.RANK_MAP.get(expertBase.getRank()));
            map.put("live", new Selector<Short,String>().putCatalog(ShortData.s0,"停用").putCatalog(ShortData.s1,"启用").run(expertBase.getLive()));
            resultMap.put("baseInfo", map);
        }

        if ("1".equals(company)) {
            ExpertCompany expertCompany = expertCompanyServices.adminSelectByExpertIdOnly(expertBase.getId());
            Map map = new HashMap();
            if (expertCompany!=null){
                map.put("code", expertCompany.getCode());
                map.put("name", expertCompany.getName());
                map.put("property",StaticConstant.COMPANY_PROPERTY_MAP.get(expertCompany.getProperty()));
                map.put("address", expertCompany.getAddress());
                map.put("phone", expertCompany.getPhone());
                map.put("affiliate", expertCompany.getAffiliate());
                map.put("affiliateCode", expertCompany.getAffiliateCode());
            }
            resultMap.put("company", map);

        }
        if ("1".equals(certificate)) {
            List<ExpertCertificate> expertCertificateList = expertCertificateServices.adminSelectByExpertId(expertBase.getId());
            List<Map> mapList = new ArrayList<Map>();
            for (ExpertCertificate expertCertificate : expertCertificateList) {
                Map map = new HashMap();
                map.put("name", expertCertificate.getName());
                map.put("num", expertCertificate.getNum());
                map.put("startTime", expertCertificate.getStartTime());
                map.put("endTime", expertCertificate.getEndTime());
                mapList.add(map);
            }
            resultMap.put("certificate", mapList);
        }

        if ("1".equals(education)) {
            List<ExpertEducation> expertEducationList = expertEducationServices.adminSelectByExpertId(expertBase.getId());
            List<Map> mapList = new ArrayList<Map>();
            for (ExpertEducation expertEducation : expertEducationList) {
                Map map = new HashMap();
                map.put("major", expertEducation.getMajor());
                map.put("learnTime", expertEducation.getLearnTime());
                map.put("school", expertEducation.getSchool());
                mapList.add(map);
            }
            resultMap.put("education", mapList);
        }

        if ("1".equals(work)) {
            List<ExpertWork> expertWorkList = expertWorkServices.adminSelectByExpertId(expertBase.getId());
            List<Map> mapList = new ArrayList<Map>();
            for (ExpertWork expertWork : expertWorkList) {
                Map map = new HashMap();
                map.put("name", expertWork.getName());
                map.put("content", expertWork.getContent());
                map.put("rank", StaticConstant.RANK_MAP.get(expertWork.getRank()));
                map.put("startTime", expertWork.getStartTime());
                map.put("endTime", expertWork.getEndTime());
                mapList.add(map);
            }
            resultMap.put("work", mapList);

        }
        if ("1".equals(review)) {
            List<ExpertReview> expertReviewList = expertReviewServices.adminSelectByExpertId(expertBase.getId());
            List<Map> mapList = new ArrayList<Map>();
            for (ExpertReview expertReview : expertReviewList) {
                Map map = new HashMap();
                map.put("company", expertReview.getCompany());
                map.put("product", expertReview.getProduct());
                map.put("role",new Selector<Short,String>().putCatalog(ShortData.s0,"组员").putCatalog(ShortData.s1,"组长").run(expertReview.getRole()) );
                map.put("startTime", expertReview.getStartTime());
                map.put("endTime", expertReview.getEndTime());
                mapList.add(map);
            }
            resultMap.put("review", mapList);
        }

        if ("1".equals(train)) {
            List<ExpertTrain> expertTrainList = expertTrainServices.adminSelectByExpertId(expertBase.getId());
            List<Map> mapList = new ArrayList<Map>();
            for (ExpertTrain expertTrain : expertTrainList) {
                Map map = new HashMap();
                map.put("name", expertTrain.getName());
                map.put("address", expertTrain.getAddress());
                map.put("signState",new Selector<Short,String>().putCatalog(ShortData.s0,"未到").putCatalog(ShortData.s1,"到").run(expertTrain.getSignState()));
                map.put("startTime", expertTrain.getStartTime());
                map.put("endTime", expertTrain.getEndTime());
                mapList.add(map);
            }
            resultMap.put("train", mapList);
        }


        if ("1".equals(major)) {
             List<MajorType> majorTypeList=   expertMajorServices.adminSelectForShow(expertBase.getId());
             resultMap.put("major", ZTree.majorType2ZTreeJSONArray(majorTypeList,false));
        }

        commonResult.setMessage(CommonResult.ZJ001_M);
        commonResult.setCode(CommonResult.ZJ001);
        commonResult.setData(resultMap);
        return commonResult;
    }


}
